package com.xr.singleton.model.dto;

import lombok.Getter;
import lombok.Setter;

/**
 * rest返回对象
 */
@Setter
@Getter
public class RestResponse<T> {

    /**
     * 服务器响应数据
     */
    private T data;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 状态码
     */
    private int code = -1;

    /**
     * 服务器响应时间
     */
    private long timestamp;

    public RestResponse() {
        this.timestamp = System.currentTimeMillis() / 1000;
    }

    public RestResponse(int code) {
        this.timestamp = System.currentTimeMillis() / 1000;
        this.code = code;
    }

    public RestResponse(int code, T data) {
        this.timestamp = System.currentTimeMillis() / 1000;
        this.code = code;
        this.data = data;
    }


    public RestResponse(int code, String msg, T data) {
        this.timestamp = System.currentTimeMillis() / 1000;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static RestResponse ok() {
        return new RestResponse(0);
    }

    public static <T> RestResponse ok(T data) {
        return new RestResponse(0, data);
    }

    public static <T> RestResponse ok(String msg, T data) {
        return new RestResponse(0, msg, data);
    }

    public static RestResponse fail() {
        return new RestResponse(-1);
    }

    public static RestResponse fail(String msg) {
        return new RestResponse(-1, msg);
    }

    public static RestResponse fail(int code) {
        return new RestResponse(-1, null);
    }

    public static RestResponse fail(int code, String msg) {
        return new RestResponse(code, msg, code);
    }

}